﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using IDAL;
using System.Collections.Generic;
using BLL;

public partial class Domain : System.Web.UI.Page
{
    public int AddOrUpdateSign = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {

            this.ddlList.DataSource = BLL.BGroup.GetMyList(0);
            this.ddlList.DataBind();

            this.ddlList.Items.Insert(0,new ListItem("请选择", "0"));
            //绑定已有列表

            
            this.gdvList.DataSource = BLL.BDomain.getList(0, null);

            string[] keys ={ "DomainId" };
            gdvList.DataKeyNames = keys;
            this.gdvList.DataBind();

            this.ddlUrl.DataSource = BLL.BUrlGroup.GetMyList();
            this.ddlUrl.DataBind();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.ddlList.SelectedValue=="0")
        {
            return  ;
        }
        Entities.Domain domain = new Entities.Domain();
        domain.Domain = txtDomain.Text.Trim();
        domain.TimeCheck = this.cbTimeTest.Checked;
        domain.GroupID = Convert.ToInt16(this.ddlList.SelectedValue);
        domain.UrlGroup = this.ddlUrl.SelectedValue;
        domain.F1 = this.txtTitle.Text.Trim();

        BLL.BDomain.Insert(domain);
        
        Response.Redirect("Domain.aspx");
    }
    protected void gdvList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int domainID = Convert.ToInt16(gdvList.DataKeys[e.NewSelectedIndex].Value);
        BLL.TestCheckMain check = new BLL.TestCheckMain();
        check.domainID = domainID;
        check.xMain();
        string testid = check.testGuid;
        ////通过以下方法获得此次测试的错误日志
        IDAL.IErrorLog errordal = IDAL.DataAccess.CreateErrorLog();
        IList<Entities.ErrorLog> list = errordal.GetTestList(domainID, testid);
        Session["TestList"] = list;
        Response.Redirect("ErrorLog.aspx");

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(this.HidDomainId.Value);
        DAL.DomainDALs domaindals = new DAL.DomainDALs();
        Entities.Domain domain = domaindals.GetByID(id);
        domain.Domain = txtDomain.Text.Trim();
        domain.TimeCheck = this.cbTimeTest.Checked;
        domain.GroupID = Convert.ToInt16(this.ddlList.SelectedValue);
        domain.UrlGroup = this.ddlUrl.SelectedValue;
        domain.F1 = this.txtTitle.Text.Trim();
        domaindals.Update(domain);
        Response.Redirect("Domain.aspx");
    }
   
    protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.RowIndex].Value);
        DAL.DomainDALs domaindals = new DAL.DomainDALs();
        Entities.Domain domain = domaindals.GetByID(id);
        domaindals.Delete(domain);
        Response.Redirect("Domain.aspx");
    }

    protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(gdvList.DataKeys[e.NewEditIndex].Value);
        DAL.DomainDALs domaindals = new DAL.DomainDALs();
        Entities.Domain doamin = domaindals.GetByID(id);
        this.txtDomain.Text = doamin.Domain;
        this.HidDomainId.Value = id.ToString();
        this.txtTitle.Text = doamin.F1;
        ddlUrl.SelectedItem.Selected = false;


        foreach (ListItem item in this.ddlUrl.Items)
        {
            if (doamin.UrlGroup == item.Value)
            {
                item.Selected = true;
            }
        }

        DAL.GroupDALs groupdals = new DAL.GroupDALs();
        Group group = groupdals.GetByID(doamin.GroupID);

        this.ddlList.DataSource = BLL.BGroup.GetMyList(0);
        this.ddlList.DataBind();

        this.ddlList.Items.Insert(0, new ListItem("请选择", "0"));
        //绑定已有列表
        
        ddlList.SelectedItem.Selected = false;

        foreach (ListItem item in this.ddlList.Items)
        {
            if (group.GroupID == int.Parse(item.Value))
            {
                item.Selected = true;
            }
        }

        this.cbTimeTest.Checked = doamin.TimeCheck;

        AddOrUpdateSign = 1;
    }

    protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvList.PageIndex = e.NewPageIndex;
        
        this.ddlList.DataSource = BLL.BGroup.GetMyList(0);
        this.ddlList.DataBind();

        this.ddlList.Items.Insert(0, new ListItem("请选择", "0"));
        //绑定已有列表


        this.gdvList.DataSource = BLL.BDomain.getList(0, null);

        string[] keys ={ "DomainId" };
        gdvList.DataKeyNames = keys;
        this.gdvList.DataBind();

        this.ddlUrl.DataSource = BLL.BUrlGroup.GetMyList();
        this.ddlUrl.DataBind();
    }
}
